package com.example.servlet;

import com.example.bean.Resident;
import com.example.dao.ResidentDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/resident")
public class ResidentServlet extends HttpServlet {
    private ResidentDAO residentDAO = new ResidentDAO();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("register".equals(action)) {
            registerResident(request, response);
        } else if ("update".equals(action) || "updateInfo".equals(action)) {
            updateResidentInfo(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("search".equals(action)) {
            searchResidents(request, response);
        } else if ("delete".equals(action)) {
            deleteResident(request, response);
        } else if ("list".equals(action)) {
            listResidents(request, response);
        }
    }



    // 登记居民信息
    private void registerResident(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String birthDateStr = request.getParameter("birthDate");
        String address = request.getParameter("address");
        String healthStatus = request.getParameter("healthStatus"); // 新增健康状态获取

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = null;
        try {
            birthDate = sdf.parse(birthDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Resident resident = new Resident();
        resident.setName(name);
        resident.setGender(gender);
        resident.setBirthDate(birthDate);
        resident.setAddress(address);
        resident.setHealthStatus(healthStatus); // 新增健康状态设置

        boolean isRegistered = residentDAO.registerResident(resident);
        if (isRegistered) {
            response.sendRedirect("resident?action=list");
        } else {
            request.setAttribute("error", "居民信息登记失败，请稍后再试");
            request.getRequestDispatcher("residentRegistration.jsp").forward(request, response);
        }
    }

    // 修改居民信息
    private void updateResidentInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String birthDateStr = request.getParameter("birthDate");
        String address = request.getParameter("address");
        String healthStatus = request.getParameter("healthStatus");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = null;
        try {
            birthDate = sdf.parse(birthDateStr);
        } catch (ParseException e) {
            request.setAttribute("error", "日期格式错误，请使用 yyyy-MM-dd 格式");
            request.getRequestDispatcher("residentEdit.jsp").forward(request, response);
            return;
        }

        Resident resident = new Resident();
        resident.setId(id);
        resident.setName(name);
        resident.setGender(gender);
        resident.setBirthDate(birthDate);
        resident.setAddress(address);
        resident.setHealthStatus(healthStatus);

        boolean isUpdated = false;
        try {
            isUpdated = residentDAO.updateResident(resident);
        } catch (Exception e) {
            request.setAttribute("error", "数据库操作出错，请稍后再试");
            request.getRequestDispatcher("residentEdit.jsp").forward(request, response);
            return;
        }

        if (isUpdated) {
            response.sendRedirect("resident?action=list");
        } else {
            request.setAttribute("error", "居民信息修改失败，请稍后再试");
            request.getRequestDispatcher("residentEdit.jsp").forward(request, response);
        }
    }
    // 查找居民信息
    private void searchResidents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<Resident> residentList = residentDAO.searchResidents(keyword);
        request.setAttribute("residentList", residentList);
        request.getRequestDispatcher("residentList.jsp").forward(request, response);
    }

    // 删除居民信息
    private void deleteResident(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean isDeleted = residentDAO.deleteResident(id);
        if (isDeleted) {
            response.sendRedirect("resident?action=list");
        } else {
            request.setAttribute("error", "居民信息删除失败，请稍后再试");
            request.getRequestDispatcher("residentList.jsp").forward(request, response);
        }
    }

    // 获取所有居民信息
    private void listResidents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Resident> residentList = residentDAO.getAllResidents();
        request.setAttribute("residentList", residentList);
        request.getRequestDispatcher("residentList.jsp").forward(request, response);
    }
}